package funtastic.spellingbee;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.media.AudioManager;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

public class ExamActivity extends Activity
{
    private DialogInterface.OnClickListener mAlertDialogCancel  = new DialogInterface.OnClickListener()
                                                                {
                                                                    @Override
                                                                    public void onClick(DialogInterface dialog, int id)
                                                                    {
                                                                        dialog.cancel();
                                                                    }
                                                                };

    private AlertDialog                     mAlertDialogDisplaySetNext;
    private AlertDialog                     mAlertDialogDisplaySetPrevious;
    private AlertDialog                     mAlertDialogGoHome;

    private int                             mCurrentIndex;

    private final int                       mDisplayCount       = 20;

    private DialogInterface.OnClickListener mDisplaySetNext     = new DialogInterface.OnClickListener()
                                                                {
                                                                    @Override
                                                                    public void onClick(DialogInterface dialog, int id)
                                                                    {
                                                                        displaySetNext();
                                                                    }
                                                                };

    private DialogInterface.OnClickListener mDisplaySetPrevious = new DialogInterface.OnClickListener()
                                                                {
                                                                    @Override
                                                                    public void onClick(DialogInterface dialog, int id)
                                                                    {
                                                                        displaySetPrevious();
                                                                    }
                                                                };

    private DialogInterface.OnClickListener mGoHome             = new DialogInterface.OnClickListener()
                                                                {
                                                                    @Override
                                                                    public void onClick(DialogInterface dialog, int id)
                                                                    {
                                                                        finish();
                                                                    }
                                                                };

    private ListView                        mListView;

    private SpellingBee                     mSpellingBee;

    private List<SpellingDetails>           mSpellingList;

    private TextView                        mTextViewScore;

    public void displaySetCurrent()
    {
        mSpellingList = mSpellingBee.getSpellingArrayList(mCurrentIndex, mCurrentIndex + mDisplayCount);
        setTitle(getTitleCurrent());
        mListView.setAdapter(new ExamAdapter(this, R.layout.list_item_exam, mSpellingList, mSpellingBee.getTextToSpeech()));
        mListView.postDelayed(new Runnable()
        {
            @Override
            public void run()
            {
                mListView.setSelection(0);
            }
        }, 100L);

        mTextViewScore.setText("0 / 0");
    }

    public void displaySetNext()
    {
        if ((mCurrentIndex + mDisplayCount) < mSpellingBee.getSpellingArrayList().size())
        {
            mCurrentIndex += 20;
            displaySetCurrent();
        }
    };

    public void displaySetNext(View view)
    {
        if (hasUnVerifiedSpellings())
        {
            mAlertDialogDisplaySetNext.show();
        }
        else
        {
            displaySetNext();
        }
    }

    public void displaySetPrevious()
    {
        if (mCurrentIndex >= mDisplayCount)
        {
            mCurrentIndex -= 20;
            displaySetCurrent();
        }
    }

    public void displaySetPrevious(View view)
    {
        if (hasUnVerifiedSpellings())
        {
            mAlertDialogDisplaySetPrevious.show();
        }
        else
        {
            displaySetPrevious();
        }
    }

    private String getTitleCurrent()
    {
        String title;

        if (mSpellingList.size() == 0)
        {
            title = mSpellingBee.getTitle() + " - 0";
        }
        else if (mSpellingList.size() < mDisplayCount)
        {
            title = mSpellingBee.getTitle() + " - " + Integer.toString(mCurrentIndex + 1) + " : " + Integer.toString(mCurrentIndex + mSpellingList.size());
        }
        else
        {
            title = mSpellingBee.getTitle() + " - " + Integer.toString(mCurrentIndex + 1) + ":" + Integer.toString(mCurrentIndex + mDisplayCount);
        }

        title += " / " + Integer.toString(mSpellingBee.getSpellingArrayList().size());

        return title;
    }

    public void goHome(View view)
    {
        if (hasUnVerifiedSpellings())
        {
            mAlertDialogGoHome.show();
        }
        else
        {
            finish();
        }
    }

    public Boolean hasUnVerifiedSpellings()
    {
        int attempted = 0;
        int verified = 0;

        for (int index = 0; index < mSpellingList.size(); index++)
        {
            if (mSpellingList.get(index).getSpellingUser().length() > 0)
            {
                attempted++;
                if (mSpellingList.get(index).getVerified() && mSpellingList.get(index).isCorrect())
                {
                    verified++;
                }
            }
        }

        return ((attempted != 0) && (attempted != verified));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        AlertDialog.Builder builder;

        super.onCreate(savedInstanceState);
        setVolumeControlStream(AudioManager.STREAM_MUSIC);

        setContentView(R.layout.exam_activity);

        mListView = (ListView) findViewById(R.id.listViewSpellings);
        mSpellingBee = (SpellingBee) getApplication();
        mTextViewScore = (TextView) findViewById(R.id.textViewScore);

        builder = new AlertDialog.Builder(this);
        builder.setMessage("There are unverified spellings in this set. Do you want to continue?");
        builder.setCancelable(false);
        builder.setPositiveButton("Get Next Set", mDisplaySetNext);
        builder.setNegativeButton("Cancel", mAlertDialogCancel);
        mAlertDialogDisplaySetNext = builder.create();

        builder = new AlertDialog.Builder(this);
        builder.setMessage("There are unverified spellings in this set. Do you want to continue?");
        builder.setCancelable(false);
        builder.setPositiveButton("Get Previous Set", mDisplaySetPrevious);
        builder.setNegativeButton("Cancel", mAlertDialogCancel);
        mAlertDialogDisplaySetPrevious = builder.create();

        builder = new AlertDialog.Builder(this);
        builder.setMessage("There are unverified spellings in this set. Do you want to continue?");
        builder.setCancelable(false);
        builder.setPositiveButton("Go Home", mGoHome);
        builder.setNegativeButton("Cancel", mAlertDialogCancel);
        mAlertDialogGoHome = builder.create();

        mCurrentIndex = 0;
        displaySetCurrent();
    }

    public void playHint(View view)
    {
        mSpellingBee.getTextToSpeech().speak(view.getTag().toString(), TextToSpeech.QUEUE_FLUSH, null);
    }

    public void verifySpellings(View view)
    {
        int attempted = 0;
        int successful = 0;

        for (int index = 0; index < mSpellingList.size(); index++)
        {
            if (mSpellingList.get(index).getSpellingUser().length() > 0)
            {
                attempted++;
                mSpellingList.get(index).setVerified(true);
                if (mSpellingList.get(index).isCorrect())
                {
                    successful++;
                }
            }
        }

        mTextViewScore.setText(Integer.toString(successful) + " / " + Integer.toString(attempted));

        ((ExamAdapter) mListView.getAdapter()).notifyDataSetChanged();
    }
}
